#include "addmyvideo.h"
#include "ui_addmyvideo.h"
#include<QFileDialog>
addmyvideo::addmyvideo(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::addmyvideo)
{
    ui->setupUi(this);
    me=new QMediaPlayer(this);
    QAudioOutput* au=new QAudioOutput(this);
    me->setAudioOutput(au);
}

addmyvideo::~addmyvideo()
{
    delete ui;
}

void addmyvideo::on_selectvideoaddress_clicked()
{
    QString str=QFileDialog::getOpenFileName(this,"",QDir::currentPath());
    ui->videoaddressline->setText(str);
    videoaddress=str;
}


void addmyvideo::on_selectimagebtn_clicked()
{
    QString str=QFileDialog::getOpenFileName(this,"",QDir::currentPath());
    videoimage=str;
    ui->selectimage->setPixmap(QPixmap(str));
}


void addmyvideo::on_confirm_clicked()
{
    if(!videoimage.isEmpty()&&!videoaddress.isEmpty())
    {
        videoid=ui->videoidline->text().toInt();
        me->setSource(QUrl::fromLocalFile(videoaddress));
        if(me->mediaStatus()==QMediaPlayer::NoMedia)
        {
            qDebug()<<"输入的播放源地址错误";
            close();
        }
        else
        {
            qint64 duration=me->duration();
            qDebug()<<duration;
            int   secs=duration/1000;   //秒
            int   mins=secs/60;         //分钟
            secs=secs % 60;             //余数秒
            QString sec=QString::number(secs);
            if(secs<10)
                sec="0"+sec;
            videolength=QString("%1:%2").arg(mins).arg(sec);
            emit addonemyvideo(videoid,videoaddress,videoimage,videolength);
            ui->selectimage->setPixmap(QPixmap(""));
            ui->videoaddressline->clear();
            ui->videoidline->clear();
            close();
        }

    }
}

